package com.jt.auth.service;

import org.springframework.security.core.userdetails.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    /**
     * 我们执行
     */
    @Autowired
    private RemoteUserService remoteUserService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        com.jt.auth.pojo.User user = remoteUserService.selectUserById(username);
        if (user==null)
            throw new UsernameNotFoundException("用户不存在");
        List<String> permissions = remoteUserService.selectPermission(user.getId());
        log.info("permissions {}",permissions.toString());
        User userinfo=new User(username,
        user.getPassword(),
                AuthorityUtils.createAuthorityList(permissions.toArray(new String[]{})));
        return userinfo;
    }
}
